import json

from django.shortcuts import render
from django.views import View
from django import http
from book.models import BookInfo
# Create your views here.
class BooksView(View):

    def post(self,request):
        # 保存图书
        # 1.获取前端数据
        data = request.body.decode()
        data_dict =json.loads(data)
        btitle = data_dict.get('btitle')
        bpub_date = data_dict.get('bpub_date')
        bread = data_dict.get('bread')
        bcomment = data_dict.get('bcomment')

        # 2.验证数据
        # 验证必传参数btitle和bpub_date
        if not all([btitle,bpub_date]):
            return http.JsonResponse({
                'error':"数据不完整"
            },status = 400)
        # 验证书名长度
        if len(btitle)>20:
            return http.JsonResponse({
                'error':"图书名字不能大于20字"
            },status=400)
        # 3.保存图书
        book = BookInfo.objects.create(**data_dict)
        # 4.返回结果
        return http.JsonResponse({
            'id':book.id,
            'btitle':book.btitle,
            'bpub_date':book.bpub_date,
            'bread':book.bread,
            'bcomment':book.bcomment
             })

    def get(self,request):
        """
        获取所有图书
        :param request:
        :return:
        """

        # 1.查询数据库获取所有图书
        books = BookInfo.objects.all()
        book_list = []
        # 2. 提取数据字段
        for book in books:
            book_list.append({
                'id':book.id,
                'btitle':book.btitle,
                'bpub_date':book.bpub_date,
                'bread':book.bread,
                'bcomment':book.bcomment
            })
        # 3. 返回结果
        return http.JsonResponse({
            'book_list':book_list
        })



class BookView(View):
    def get(self,request,pk):
        """
        获取单一图书
        :param request:
        :return:
        """
        # 1.根据id值查询数据库
        try:
            book = BookInfo.objects.get(id=pk,is_delete=False)
        except:
            return http.JsonResponse({
                'error':'id不存在'
            },status=400)
        # 2.返回数据
        return http.JsonResponse({
            'id': book.id,
            'btitle': book.btitle,
            'bpub_date': book.bpub_date,
            'bread': book.bread,
            'bcomment': book.bcomment
        })


    def put(self,request,pk):
        """
        更新图书
        :param request:
        :return:
        """
        # 1.获取前端数据
        data =request.body.decode()
        data_dict = json.loads(data)
        btitle = data_dict.get('btitle')
        bpub_date = data_dict.get('bpub_date')
        bread = data_dict.get('bread')
        bcomment = data_dict.get('bcomment')
        # 2.验证数据
        if btitle:
            if len(btitle) > 20 :
                return http.JsonResponse({'error':'图书名字不能大于20个字'},status=400)
        # 3. 更新图书
        res= BookInfo.objects.filter(id=pk).update(**data_dict)
        if res==0:
            return http.JsonResponse({'error':'更新失败'})
        book = BookInfo.objects.get(id=pk)

        return http.JsonResponse({
            'id': book.id,
            'btitle': book.btitle,
            'bpub_date': book.bpub_date,
            'bread': book.bread,
            'bcomment': book.bcomment
        })

    def delete(self,request,pk):
        """
        删除图书
        :param request:
        :return:
        """

        # 1.根据id值查询数据库
        try:
            book = BookInfo.objects.get(id=pk)
        except:
            return http.JsonResponse({
                'error': 'id不存在'
            }, status=400)
        # 2.删除图书。逻辑删除、物理删除    book.delete()
        book.is_delete = True
        book.save()

        return http.JsonResponse({'res':'删除成功'})